import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.File;
import java.io.IOException;

/**
 * Created by abely on 16-5-16.
 */
public class ProxyBaiDuPic {
    public static void main(String[] args) {
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        HttpHost proxyTarget = new HttpHost("172.26.100.238",64000);
        clientBuilder.setProxy(proxyTarget);
        clientBuilder.setUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        Credentials defaultcreds = new UsernamePasswordCredentials("abely_liu", "Welcome5");
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, defaultcreds);
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
        CloseableHttpClient httpClient = clientBuilder.build();
        String url = "http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=%E5%A3%81%E7%BA%B8&cg=wallpaper&pn=30&rn=30&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=6000";
        int x = 0x0001e;
        int name =1;
        for(int i=0;i<1;i++){
            x = x + 18;
            String targetUrl = url+x;
            HttpGet httpGet = new HttpGet(targetUrl);
            HttpEntity httpEntity = null;
            try {
                httpEntity = httpClient.execute(httpGet).getEntity();
                JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(httpEntity,"utf-8"));
                JSONArray jsonArray = JSONObject.parseArray(jsonObject.get("imgs").toString());
                for (int j = 0; j < jsonArray.size(); j++) {
                    String targetUrl2 = JSONObject.parseObject(jsonArray.get(j).toString()).get("hoverURL").toString();
                    System.out.println(targetUrl2);
                    HttpGet httpGet1 = new HttpGet(targetUrl2);
                    try {
                        HttpEntity httpEntity1 = httpClient.execute(httpGet1).getEntity();
                        File file = new File("/home/abely/Pic/"+name);
                        FileUtils.writeByteArrayToFile(file,EntityUtils.toByteArray(httpEntity1));
                        name++;
                    }catch (Exception e){
                        continue;
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
